using System;
using System.Collections.Generic;
using System.Text;
using PSLibrary = Microsoft.Office.Project.Server.Library;
using System.Web.Services.Protocols;
using System.Globalization;

namespace Microsoft.Office.Project.EPMSync.Config.Library
{
    public static class PSError
    {
        public static string FormattedMessage(SoapException ex)
        {
            if (ex != null)
            {
                StringBuilder message = new StringBuilder();
                PSLibrary.PSClientError error = new PSLibrary.PSClientError(ex);
                PSLibrary.PSErrorInfo[] errors = error.GetAllErrors();
                for (int i = 0; i < errors.Length; i++)
                {
                    message.AppendFormat(CultureInfo.InvariantCulture, "{0} \r\nPSClientError => {1}\r\n", ex.Message, errors[i].ErrId.ToString());

                    for (int j = 0; j < errors[i].ErrorAttributes.Length; j++)
                    {
                        message.AppendFormat("\r\n\t{0}: {1}",errors[i].ErrorAttributeNames()[j], errors[i].ErrorAttributes[j]);
                    }
                }
                return message.ToString();
            }
            return String.Empty;
        }
    }
}
